import WeatherBox from '../WeatherBox/WeatherBox';

export default class RoadWeatherHandler {
    constructor(map) {
        this.chart = null;
        this.infoObj = null;
        this.areaName = '';
        this.adcode = '';
        this.map = map;
    }

    loadData(isInitChart, data) {
        let that = this;
        let areaName = data ? data.data.city : '';
        if(/市辖区$/.test(areaName)) {
            areaName = data ? data.data.province : '';
        }
        const adcode = data ? data.data.adcode : '';
        minemap.util.getJSON('http://203.81.245.38:48082/weather/adminweather/' + adcode, function (error, data) {
            if (error) {
                console.log(error)
            } else {
                that.infoObj = data;
                that.areaName = areaName;
                that.adcode = adcode;
                if (isInitChart) {
                    that.updateChart();
                }
            }
        });
        // let that = this;
        // minemap.util.getJSON('../data/weather.json', function (error, data) {
        //     if (error) {
        //         console.log(error)
        //     } else {
        //         that.infoObj = data;
        //         that.areaName = '北京市';
        //         that.adcode = '110000';
        //         if (isInitChart) {
        //             that.updateChart();
        //         }
        //     }
        // });
    }

    updateChart() {
        document.getElementById('weather-chart').innerHTML = '';
        this.chart = new WeatherBox('weather-chart', this.infoObj, this.areaName, this.adcode, this.map);
    }

    getInfoObj() {
        return this.infoObj;
    }

    getChart() {
        return this.chart;
    }
}